#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：api_testing 
@File    ：assert_function.py
@IDE     ：PyCharm 
@Author  ：future76
@Date    ：2024/6/26 9:21 
@Desc    ：断言方法
"""
# 标准库导入
import ast
from typing import Any, Union, Text
from datetime import datetime
# 第三方库导入
import allure


@allure.step("预期结果：{expect_value}  == 实际结果：{actual_value}")
def eq(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    判断是否相等
    """
    assert expect_value == actual_value, message


@allure.step("预期结果：{expect_value} < 实际结果：{actual_value}")
def less_than(expect_value: Union[int, float], actual_value: Union[int, float], message: Text = ""):
    """
    判断预期结果小于实际结果
    """
    assert expect_value < actual_value, message


@allure.step("预期结果：{expect_value} <= 实际结果：{actual_value}")
def less_than_or_equals(expect_value: Union[int, float], actual_value: Union[int, float], message: Text = ""):
    """
    判断预期结果小于等于实际结果
    """
    assert expect_value <= actual_value, message


@allure.step("预期结果：{expect_value} > 实际结果：{actual_value}")
def greater_than(expect_value: Union[int, float], actual_value: Union[int, float], message: Text = ""):
    """
    判断预期结果大于实际结果
    """
    assert expect_value > actual_value, message


@allure.step("预期结果：{expect_value} >= 实际结果：{actual_value}")
def greater_than_or_equals(expect_value: Union[int, float], actual_value: Union[int, float], message: Text = ""):
    """
    判断预期结果大于等于实际结果
    """
    assert expect_value >= actual_value, message


@allure.step("预期结果：{expect_value} != 实际结果：{actual_value}")
def not_equals(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    判断预期结果不等于实际结果
    """
    assert expect_value != actual_value, message


@allure.step("预期结果：{expect_value}  == 实际结果：{actual_value}")
def string_equals(expect_value: Any, actual_value: Text, message: Text = ""):
    """
    判断字符串是否相等
    """
    assert expect_value == actual_value, message


@allure.step("长度相等 --> 预期结果：{expect_value}  == 实际结果：{actual_value}")
def length_equals(expect_value: int, actual_value: Text, message: Text = ""):
    """
    判断长度是否相等
    """
    assert isinstance(
        expect_value, int
    ), "expect_value 需要为 int 类型"
    assert expect_value == len(actual_value), message


@allure.step("长度大于 --> 预期结果：{expect_value}  > 实际结果：{actual_value}")
def length_greater_than(expect_value: Union[int, float], actual_value: Text, message: Text = ""):
    """
    判断长度大于
    """
    assert isinstance(
        expect_value, (float, int)
    ), "expect_value 需要为 float/int 类型"
    assert expect_value > len(str(actual_value)), message


@allure.step("长度大于等于 --> 预期结果：{expect_value}  >= 实际结果：{actual_value}")
def length_greater_than_or_equals(expect_value: Union[int, float], actual_value: Text, message: Text = ""):
    """
    判断长度大于等于
    """
    assert isinstance(
        expect_value, (int, float)
    ), "expect_value 需要为 float/int 类型"
    assert expect_value >= len(actual_value), message


@allure.step("长度小于 --> 预期结果：{expect_value}  < 实际结果：{actual_value}")
def length_less_than(expect_value: Union[int, float], actual_value: Text, message: Text = ""):
    """
    判断长度小于
    """
    assert isinstance(
        expect_value, (int, float)
    ), "expect_value 需要为 float/int 类型"
    assert expect_value < len(actual_value), message


@allure.step("长度小于等于 --> 预期结果：{expect_value}  <= 实际结果：{actual_value}")
def length_less_than_or_equals(expect_value: Union[int, float], actual_value: Text, message: Text = ""):
    """判断长度小于等于"""
    assert isinstance(
        expect_value, (int, float)
    ), "expect_value 需要为 float/int 类型"
    assert expect_value <= len(actual_value), message


@allure.step("预期结果：{expect_value}  in 实际结果：{actual_value}")
def contains(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    判断预期结果内容被实际结果包含
    """
    assert isinstance(
        actual_value, (list, tuple, dict, str, bytes)
    ), "actual_value 需要为  list/tuple/dict/str/bytes  类型"
    assert expect_value in actual_value, message


@allure.step("实际结果：{actual_value}  in 预期结果：{expect_value}")
def contained_by(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    判断预期结果包含实际结果
    """
    assert isinstance(
        actual_value, (list, tuple, dict, str, bytes)
    ), "actual_value 需要为  list/tuple/dict/str/bytes  类型"

    assert actual_value in expect_value, message


@allure.step("实际结果：{actual_value}  是以  预期结果： {expect_value} 开头的")
def startswith(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查实际结果的开头是否和预期结果内容的开头相等
    """
    assert str(actual_value).startswith(str(expect_value)), message


@allure.step("实际结果：{actual_value}  是以  预期结果： {expect_value} 结尾的")
def endswith(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查实际结果的结尾是否和预期结果内容相等
    """
    assert str(actual_value).endswith(str(expect_value)), message


@allure.step("实际结果：{actual_value}  中全部是  预期结果： {expect_value} ")
def one_to_many(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查实际结果是否全部是预期结果内容，一对多
    """
    assert len(set(actual_value)) == 1 and str(actual_value[0]) == str(expect_value), message


@allure.step("实际结果：{actual_value}  中的数据类型和  预期结果： {expect_value}一致 ")
def data_type(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查实际结果的数据类型是否和预期结果的数据类型一致
    """
    assert isinstance(actual_value, type(expect_value)), message


@allure.step("实际结果：{actual_value}  中的数据包含  预期结果： {expect_value} ")
def is_all_contains(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查预期结果(列表)中的数据是否在实际结果中全都有
    """
    assert set(expect_value).issubset(set(actual_value)),message


@allure.step("预期结果：{expect_value} >= 实际结果：{actual_value}的最大值")
def greater_than_all_or_equals(expect_value: Union[int, float], actual_value: Any, message: Text = ""):
    """
    检查预期结果大于等于实际结果(列表)的最大值
    """
    assert expect_value >= int(max(actual_value)), message


@allure.step("预期结果：{expect_value} 年份等于 实际结果：{actual_value}的年份")
def year_equals(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查预期结果的年份等于实际结果的所有年份
    """
    data = [i[0:4] for i in actual_value]
    assert len(set(data)) == 1 and str(data[0]) == str(expect_value), message


@allure.step("实际结果：{actual_value} 日期在  预期结果： {expect_value}的范围内 ")
def is_date_range(expect_value: list, actual_value: list, message: Text = ""):
    """
    检查实际结果的日期在预期日期的范围内
    """
    data = [i[0:10] for i in actual_value]
    start_date = datetime.strptime(expect_value[0], "%Y-%m-%d")
    end_date = datetime.strptime(expect_value[1], "%Y-%m-%d")
    check_date = [True for d in data if start_date <= datetime.strptime(d, "%Y-%m-%d") <= end_date]
    assert len(set(check_date)) == 1 and check_date[0] is True, message


@allure.step("预期结果：{expect_value}  in 实际结果：{actual_value}中的每个值")
def contains_all(expect_value: Any, actual_value: Any, message: Text = ""):
    """
    检查预期结果内容是否被实际结果(列表)中的每一个值包含(模糊查询)
    """
    assert isinstance(
        actual_value, (list, tuple, dict, str, bytes)
    ), "actual_value 需要为  list/tuple/dict/str/bytes  类型"
    assert expect_value in actual_value, message
    check_date = [True for i in actual_value if expect_value in i]
    assert len(set(check_date)) == 1 and check_date[0] is True, message


@allure.step("实际结果：{actual_value} 在  预期结果： {expect_value}的范围内 ")
def is_int_range(expect_value: list, actual_value: list, message: Text = ""):
    """
    检查实际结果在预期结果的范围内
    """
    min_data = int(expect_value[0])
    max_data = int(expect_value[1])
    check_date = [True for i in actual_value if min_data <= i <= max_data]
    assert len(set(check_date)) == 1 and check_date[0] is True, message

if __name__ == '__main__':
    a = [1,2,3,8]
    b = [1,2,3,4,5,7]
    print(set(a).issubset(set(b)))

    print(b[0:4])
    d = ["2024-01-19 22:21:16","2025-01-19 22:21:16","2026-01-19 22:21:16","2027-01-19 22:21:16","2028-01-19 22:21:16","2029-01-19 22:21:16"]
    c = [i[0:10] for i in d]
    print(c)
    expect_value = ["2024-01-19","2029-01-19"]

    start_date = datetime.strptime(expect_value[0], "%Y-%m-%d")
    end_date = datetime.strptime(expect_value[1], "%Y-%m-%d")
    e = []
    for d in c:
        check_date = datetime.strptime(d, "%Y-%m-%d")
        if start_date <= check_date <= end_date:
            f = True
            e.append(f)
    print(e)
    print(len(set(e)) == 1 and e[0] is True)


    aaa = "小满乙1二n醇Iw丙P二醇二丙二醇"
    bbb = "小满"

    print(bbb in aaa)


    m = ''
    n = "3"
    print(n != m)
